<template>
  <div class="tm-colors">
    <div v-for="(item, index) in currValue"
         :key="index"
         class="tm-colors-item"
         >
      <el-color-picker :value="item" show-alpha @change="(val) => {
        onChange(val, index)
      }"></el-color-picker>
    </div>
   <div class="tm-colors-plus" @click="onAdd">
      <i class="el-icon-plus"></i>
   </div>
  <div class="tm-colors-plus" @click="onRemove">
    <i class="el-icon-minus"></i>
  </div>
  </div>
</template>

<script>

export default {
  name: 'tm-colors',
  cnName: '颜色组',
  props: {
    value: Array
  },
  data() {
    return {
        currValue: this.value
    }
  },
  methods: {
    emit() {
      this.$emit('input', [].concat(this.currValue))
    },
    onChange(val, index) {
      this.currValue[index] = val
      this.emit()
    },
    onAdd() {
      this.currValue.push('')
      this.emit()
    },
    onRemove() {
      this.currValue.pop()
      this.emit()
    }
  },
  watch: {
    value(val) {
      this.currValue = val
    }
  }
}
</script>

<style lang="less" scoped>
.tm-colors {
  color: var(--text-color);
  display: inline-flex;
  flex-wrap: wrap;

  &-item {
    width: 28px;
    height: 28px;
    margin-right: 10px;
    margin-bottom: 10px;
    display: inline-block;
    position: relative;
  }

  &-plus {
    width: 28px;
    height: 28px;
    border: 1px solid #e6e6e6;
    border-radius: 4px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
    margin-right: 10px;
    margin-bottom: 10px;
  }
}
</style>
